/*
 * SocketClient.cpp
 *
 *  Created on: Aug 9, 2017
 *      Author: guoxs
 */
#include "cfile.h"
#include "utils/Log.h"

cFile::cFile(std::string filename) {
	m_name = filename;
	LOGD("cFile %s",m_name.c_str());
}

cFile::~cFile() {
}

void cFile::write(std::string str) {
	bool ret= true;
	m_fp = fopen(m_name.c_str(), "w");
	if (m_fp != NULL) {
		LOGD(" file length:%d!!!",str.length());
		fputs(str.c_str(),m_fp);
		fclose(m_fp);

	}else{
		LOGD(" file error!!!");
	}
	return ;
}

std::string cFile::read() {
	m_fp = fopen(m_name.c_str(), "r");
	if (m_fp != NULL) {
		if(fgets(m_read,4096,m_fp) != NULL){
			m_readstr = m_read;
		}
		fclose(m_fp);
	}else{
		LOGD(" file error!!!");
	}
	return m_readstr;
}

bool cFile::isExist() {
}

void cFile::write(std::string filename, std::string str) {
	bool ret= true;
	m_fp = fopen(filename.c_str(), "w");
	if (m_fp != NULL) {
		LOGD(" file length:%d!!!",str.length());
		fputs(str.c_str(),m_fp);
		fclose(m_fp);

	}else{
		LOGD(" file error!!!");
	}
	return ;
}

std::string cFile::read(std::string filename) {
	m_fp = fopen(filename.c_str(), "r");
	if (m_fp != NULL) {
		if(fgets(m_read,4096,m_fp) != NULL){
			m_readstr = m_read;
		}
		fclose(m_fp);
	}else{
		LOGD(" file error!!!");
	}
	return m_readstr;
}

struct stat* cFile::getFileStat() {
	int result  = stat(m_name.c_str(),&filestat);
	if(result != 0){
		LOGD(" file error!!!");
		return NULL;
	}
	LOGD(" file stat:%s",ctime(&filestat.st_ctime));
	return &filestat;
}

int cFile::read(unsigned char* buff, int maxsize) {
	int ret;
	m_fp = fopen(m_name.c_str(), "rb+");
	if (m_fp != NULL) {
		ret = fread(buff,1,maxsize,m_fp);
		fclose(m_fp);
	}else{
		LOGD(" file error!!!");
	}
	return ret;
}

bool cFile::deleteFile(std::string file){
	remove(file.c_str());
	return false;
}
